package com.example.sunmiv1.adapter;

import android.content.Context;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.callback.RVCallback;
import com.example.sunmiv1.entity.GoodsStoreRetreatInfo;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;

import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PurchaseRefundChooseGoodsAdapter extends RecyclerView.Adapter<PurchaseRefundChooseGoodsAdapter.prcgVholder>{
    private List<GoodsStoreRetreatInfo.DataBean> list;
    private Context mContext;
    private CheckBoxListener mCheckBoxListener;
    private RVCallback mRVCallback;
    private String pricePermission;// 門店查看價格權限

    public PurchaseRefundChooseGoodsAdapter(List<GoodsStoreRetreatInfo.DataBean> mList, Context context) {
        super();
        list = mList;
        mContext = context;
        ACache aCache = ACache.get(context);
        pricePermission = aCache.getAsString("showPrice");
    }

    @Override
    public prcgVholder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_purchase_refund_add, parent, false);
        return new prcgVholder(view);
    }

    @Override
    public void onBindViewHolder(prcgVholder holder, int position) {
        holder.goodName.setText(list.get(position).getGoods_name());
        Tools.hideViewInItem(list.get(position).getSku_name(),holder.skuName);
        holder.goodsNumber.setText(list.get(position).getGoodsNumByAttr());
        holder.goodsCode.setText(list.get(position).getGoodsCodeByAttr());
        holder.infoPurQuantity.setText(String.valueOf(list.get(position).getPurchase_num()));
        holder.infoReturnedQuantity.setText(String.valueOf(list.get(position).getRetreat_num()));
        holder.infoCanReturnQuantity.setText(String.valueOf(list.get(position).getSurplus_num()));
        holder.infoPurchasePrice.setText(String.valueOf(list.get(position).getPurchase_price()));
        holder.infoOrderNumber.setText(String.valueOf(list.get(position).getOrder_number()));
        holder.goodsUnit.setText(list.get(position).getCompany());

        holder.infoPurBuildDate.setText(list.get(position).getBuild_date());
        holder.infoPurQualityGuaranteePeriod.setText(list.get(position).getQuality_guarantee_period());

        Tools.hideViewInItem(list.get(position).getCompany(),holder.goodsUnit);
        holder.addGoodsCheck.setChecked(list.get(position).isChecked());
        holder.addGoodsCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mCheckBoxListener.setCheckBoxListener(isChecked, position);
            }
        });
        ((View)holder.edRetreatPrice.getParent()).setVisibility(View.GONE);
        ((View)holder.edRetreatNumAct.getParent()).setVisibility(View.GONE);
        ((View)holder.edRetreatItemNote.getParent()).setVisibility(View.GONE);
        if ("2".equals(pricePermission)) {
            ((View) holder.infoPurchasePrice.getParent()).setVisibility(View.GONE);
            ((View) holder.edRetreatPrice.getParent()).setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    static class prcgVholder extends RecyclerView.ViewHolder {
        @BindView(R.id.add_goods_check)
        CheckBox addGoodsCheck;
        @BindView(R.id.goods_name)
        TextView goodName;
        @BindView(R.id.layout_goods_name)
        RelativeLayout layoutGoodsName;
        @BindView(R.id.sku_name)
        TextView skuName;
        @BindView(R.id.layout_sku_name)
        RelativeLayout layoutSkuName;
        @BindView(R.id.goods_number)
        TextView goodsNumber;
        @BindView(R.id.layout_goods_number)
        RelativeLayout layoutGoodsNumber;
        @BindView(R.id.goods_code)
        TextView goodsCode;
        @BindView(R.id.goods_unit)
        TextView goodsUnit;
        @BindView(R.id.layout_goods_code)
        RelativeLayout layoutGoodsCode;
        @BindView(R.id.info_pur_quantity)
        TextView infoPurQuantity;
        @BindView(R.id.info_returned_quantity)
        TextView infoReturnedQuantity;
        @BindView(R.id.info_can_return_quantity)
        TextView infoCanReturnQuantity;
        @BindView(R.id.info_purchase_price)
        TextView infoPurchasePrice;
        @BindView(R.id.info_order_number)
        TextView infoOrderNumber;
        @BindView(R.id.ed_retreat_price)
        EditText edRetreatPrice;
        @BindView(R.id.ed_retreat_num_act)
        EditText edRetreatNumAct;
        @BindView(R.id.ed_retreat_item_note)
        EditText edRetreatItemNote;
        @BindView(R.id.info_pur_build_date)
        TextView infoPurBuildDate;
        @BindView(R.id.info_pur_quality_guarantee_period)
        TextView infoPurQualityGuaranteePeriod;

        public prcgVholder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    // 全選
    public void checkAll() {
        Iterator<GoodsStoreRetreatInfo.DataBean> iterable = list.iterator();
        while (iterable.hasNext()) {
            iterable.next().setChecked(true);
        }
        notifyDataSetChanged();
    }

    // 全不選
    public void checkAllNot() {
        Iterator<GoodsStoreRetreatInfo.DataBean> iterable = list.iterator();
        while (iterable.hasNext()) {
            iterable.next().setChecked(false);
        }
        notifyDataSetChanged();
    }

    // 移除一個
    public void removeOne(int position) {
        list.remove(position);
    }

    /* 移除多個 */
    public void removeMore(SparseArray<Integer> positions) {
//        list.r
    }


    public static interface CheckBoxListener {
        void setCheckBoxListener(boolean isChecked, int position);
    }

    public void setCBListener(CheckBoxListener cbListener) {
        this.mCheckBoxListener = cbListener;
    }

    public void setRVCallback(RVCallback rvCallback){
        this.mRVCallback=rvCallback;
    }
}
